import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../enum/item_enum.dart';

///
/// 子项类型
///
enum LxItemContentType {
  /// 箭头
  arrow,

  /// 文本+箭头
  textArrow,

  /// 文本
  text,

  /// 开关
  sw,

  /// 无
  none,

  /// 颜色+文本
  colorText,
}

///
/// 转换后的设置项数据模型
///
class LxItemModel {
  /// ID
  int? id;

  /// key
  String? key;

  /// 标题
  String? title;

  /// 头部标题
  String? headerTitle;

  /// 头部说明
  LxItemHeaderExtraModel? headerExtra;

  /// 内容类型
  LxItemContentType? contentType;

  /// 子列表
  List<dynamic>? child = [];

  /// 显示信息
  RxString? value = '--'.obs;

  /// 开关
  RxBool? switchValue = false.obs;

  /// 列表附加信息 - 颜色（L8氛围灯）
  Rxn<int>? extraColor = Rxn();

  /// 附加信息 - 图标
  Rxn<IconData>? extraIcon = Rxn();

  /// 是否收藏
  int? like;

  /// 单位
  LxDataUnit? unit;

  /// 禁止交互，内容和箭头颜色置灰
  RxBool disEnable = false.obs;

  /// 列表说明
  LxItemHeaderExtraModel? extra;

  LxItemModel(
      {id,
      key,
      title,
      headerTitle,
      headerExtra,
      contentType,
      child,
      value,
      switchValue,
      extraColor,
      extraIcon,
      like,
      unit,
      required disEnable,
      extra});

  LxItemModel.fromJson(Map<dynamic, dynamic>? json) {
    if (json == null) {
      return;
    }
    id = json['id'];
    key = json['key'];
    title = json['name'];
    int contentTypeIndex = 0;
    if (json['type'] is int && json['type'] < LxItemContentType.values.length) {
      contentTypeIndex = json['type'];
    }
    // 根据ContentType的索引获取相应类型
    contentType = LxItemContentType.values[contentTypeIndex];
    like = json['favorite'];
    if (json['extra'] != null) {
      extra = LxItemHeaderExtraModel.fromJson(json['extra']);
    }
  }

  LxItemModel copy() {
    return LxItemModel(
        id: id,
        key: key,
        title: title,
        headerTitle: headerTitle,
        headerExtra: headerExtra,
        contentType: contentType,
        child: child,
        value: value,
        switchValue: switchValue,
        extraColor: extraColor,
        like: like,
        unit: unit,
        disEnable: disEnable,
        extra: extra);
  }
}

///
/// 附加字段
/// 标题：在列表标题后显示图标，点击后跳转至相应链接
///
/// 列表：存在icon时在标题后显示图标，存在url时跳转webview，存在description时弹框
///
class LxItemHeaderExtraModel {
  /// 存在icon时，优先显示相应的图标
  String? icon;

  /// 按钮标题
  String? entry;

  /// 存在url时，优先跳转链接
  String? url;

  /// 存在description时，显示弹框
  String? description;

  LxItemHeaderExtraModel({url, icon, description});
  LxItemHeaderExtraModel.fromJson(Map<dynamic, dynamic>? json) {
    if (json == null) {
      return;
    }
    entry = json['entry'];
    icon = json['icon'];
    url = json['href'];
    description = json['description'];
  }
}

///
/// 设置项数据模型（单个分组）
///
class VehicleSettingListItemModel {
  /// 分组名称
  String? name;

  /// 该分组下的功能列表
  List? list;

  /// 分组附加字段
  LxItemHeaderExtraModel? extra;

  VehicleSettingListItemModel({
    name,
    list,
    extra,
  });

  VehicleSettingListItemModel.fromJson(Map<dynamic, dynamic> json) {
    name = json['name'];
    list = json['list'];
    extra = LxItemHeaderExtraModel.fromJson(json['extra']);
  }
}

///
/// 设置项数据模型
///
class VehicleSettingListModel {
  /// 分类标识
  String? category;

  /// 分类名
  String? header;

  /// 分类图标地址
  Map<dynamic, dynamic>? icon;

  /// 该分类下功能分组列表
  List<dynamic>? list;

  /// 筛选后的单页详细功能列表
  List<List<LxItemModel>>? pageList;
  VehicleSettingListModel({
    category,
    header,
    icon,
    list,
    pageList,
  });

  VehicleSettingListModel.fromJson(Map<dynamic, dynamic> json) {
    category = json['category'];
    header = json['name'];
    icon = json['icon'];
    list = json['list'];
  }
}
